package com.example.demo.module.develop.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.entity.response.Rs;
import com.example.demo.constant.Constant;
import com.example.demo.entity.pojo.develop.DevelopBomPoJo;
import com.example.demo.entity.request.develop.DevelopBomRequest;
import com.example.demo.entity.response.develop.DevelopBomResponse;
import com.example.demo.module.develop.service.DevelopBomService;
import com.example.demo.utils.PoJoUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author lzy
 * @date 2020-09-09 17:28:01
 */
@RestController
@Api(tags = "研发BOM表接口")
@RequestMapping("/develop/bom")
public class DevelopBomController {
    @Resource
    private DevelopBomService service;


    /**
     * 分页查询
     */
    @GetMapping
    @ApiOperation(value = "分页查询研发BOM表", notes = "根据条件返回集合对象(默认page=1,size=10)")
    public ResponseEntity<Rs<List<DevelopBomResponse>>> page(DevelopBomRequest request,@ApiIgnore @RequestParam(Constant.HeaderKey.COMPANY_ID) Long companyId) {
        DevelopBomPoJo poJo = new DevelopBomPoJo();
        BeanUtils.copyProperties(request, poJo);
        poJo.setCompanyId(companyId);
        Page<DevelopBomPoJo> result = service.findPage(request.getPage(), request.getSize(), poJo);
        List<DevelopBomResponse> list = PoJoUtil.PageUtil(result, DevelopBomResponse.class);
        return Rs.ok(Rs.json().put("PurchaseBomResponse", list).put("pages", PoJoUtil.Pages(result.getTotal(), request.getSize())));
    }

    /**
     * 垃圾篓的查询
     */
    @GetMapping("/bList")
    @ApiOperation(value = "查询垃圾篓的BOM表", notes = "根据条件返回集合对象")
    public ResponseEntity<Rs<List<DevelopBomResponse>>> blist(@ApiParam(name = "name", value = "产品名称模糊搜索的时候用") @RequestParam(value = "name", required = false) String name,
                                                              @ApiIgnore @RequestParam(Constant.HeaderKey.COMPANY_ID) Long companyId) {
        DevelopBomPoJo poJo = new DevelopBomPoJo();
        poJo.setDeleteFlag(true);
        poJo.setName(name);
        poJo.setCompanyId(companyId);
        List<DevelopBomPoJo> result = service.findList(poJo);
        return Rs.ok(Rs.json().put("DevelopBomResponse", PoJoUtil.transToPoJo(result, DevelopBomResponse.class)));
    }

    /**
     * 查询
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询研发BOM表", notes = "根据条件返回集合对象")
    public ResponseEntity<Rs<List<DevelopBomResponse>>> list(DevelopBomRequest request,@ApiIgnore @RequestParam(Constant.HeaderKey.COMPANY_ID) Long companyId) {
        DevelopBomPoJo poJo = new DevelopBomPoJo();
        BeanUtils.copyProperties(request, poJo);
        poJo.setCompanyId(companyId);
        List<DevelopBomPoJo> result = service.findList(poJo);
        return Rs.ok(Rs.json().put("listDevelopBomResponse", PoJoUtil.transToPoJo(result, DevelopBomResponse.class)));
    }

    /**
     * 批量添加
     */
    @PostMapping("/batch")
    @ApiOperation(value = "批量添加研发BOM表", notes = "返回msg")
    public ResponseEntity<Rs<Object>> batchadd(@ApiParam(name = "List<PurchaseBomPoJo>", value = "多个BOM表批量添加，需要json数组") @RequestBody List<DevelopBomRequest> request,
                                               @ApiIgnore @RequestParam(Constant.HeaderKey.COMPANY_ID) Long companyId) {
        service.createBatch(PoJoUtil.transToPoJo(request, DevelopBomPoJo.class), companyId);
        return Rs.ok();
    }

    /**
     * 单个添加
     */
    @PostMapping
    @ApiOperation(value = "单个添加研发BOM表", notes = "返回msg")
    public ResponseEntity<Rs<Long>> add(@ApiParam(name = "PurchaseBomPoJo", value = "单个BOM表添加并且返回bomId") @RequestBody DevelopBomRequest request,
                                        @ApiIgnore @RequestParam(Constant.HeaderKey.COMPANY_ID) Long companyId) {
        DevelopBomPoJo poJo = new DevelopBomPoJo();
        BeanUtils.copyProperties(request, poJo);
        poJo.setCompanyId(companyId);
        return Rs.ok(Rs.json().put("bomId", service.create(poJo).getId()));
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation(value = "修改研发BOM表", notes = "返回msg")
    public ResponseEntity<Rs<Object>> update(@ApiParam(name = "bomId", value = "根据id更新bom表", required = true) @RequestBody DevelopBomRequest request,
                                             @ApiIgnore @RequestParam(Constant.HeaderKey.COMPANY_ID) Long companyId) {
        DevelopBomPoJo poJo = new DevelopBomPoJo();
        BeanUtils.copyProperties(request, poJo);
        poJo.setCompanyId(companyId);
        service.update(poJo);
        return Rs.ok();
    }

    /**
     * 彻底批量删除
     */
    @DeleteMapping("/batch/{ids}")
    @ApiOperation(value = "批量彻底删除研发BOM表", notes = "返回msg")
    public ResponseEntity<Rs<Object>> batchdelete(@ApiParam(name = "ids", value = "根据ids彻底删除bom表以及对应的研发BOM物料表，需要json格式ids数组", required = true) @PathVariable("ids") List<Long> ids) {
        service.batchRemove(ids);
        return Rs.ok();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "批量删除研发BOM表", notes = "返回msg")
    public ResponseEntity<Rs<Object>> batchremove(@ApiParam(name = "ids", value = "根据ids删除bom表以及对应的研发BOM物料表，需要json格式ids数组", required = true) @PathVariable("ids") List<Long> ids) {
        service.remove(ids);
        return Rs.ok();
    }


}
